package com.mingyizhudao.app.view.recommend;

import java.util.Map;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.entity.DeptEntity;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.view.MYBaseActivity;

public class DeptIntroduceActivity extends MYBaseActivity implements
		OnClickListener {
	private String deptUrl, deptId, facultyName, hospitalName;

	private TextView dept_name, dept_introduce;
	private Button btn_order;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_dept_introduce);
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void initHeaderView() {

		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView() {
		hospitalName = getIntent().getStringExtra("hospitalName");
		deptUrl = getIntent().getStringExtra("deptUrl");
		facultyName = getIntent().getStringExtra("facultyName");
		deptId = getIntent().getStringExtra("deptId");
		setTitleViewValue(0, 0, R.color.white);
		// setTitleViewValue( hospitalName );

		dept_name = (TextView) findViewById(R.id.dept_name);
		dept_introduce = (TextView) findViewById(R.id.dept_introduce);
		btn_order = (Button) findViewById(R.id.btn_order);

		// dept_name.setText(facultyName);
		setTitleViewValue(facultyName);
		btn_order.setOnClickListener(this);

		getDeptIntro();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_order:
			CommonUtils.goToCreateOrder(activity, deptId, null, facultyName,
					hospitalName, null, true);
			break;
		default:
			break;
		}
	}

	private void getDeptIntro() {
		LoadingDialog.getInstance(activity).show();
		QJNetUICallback callback = new QJNetUICallback<QjResult<Map<String, DeptEntity>>>(
				activity) {
			@Override
			public void onSuccess(final QjResult<Map<String, DeptEntity>> result) {
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						Map<String, DeptEntity> map = result.getResults();
						if (map == null)
							return;
						DeptEntity deptEntity = map.get("department");
						if (deptEntity != null) {
							setTitleViewValue(deptEntity.getHpName());
							// dept_name.setText(deptEntity.getName());
							setTitleViewValue(deptEntity.getName());
							String des = deptEntity.getDescription();
							if (des != null && des.length() > 0) {
								dept_introduce.setText("科室介绍："
										+ deptEntity.getDescription());
							} else {
								dept_introduce.setText("暂无介绍");
							}

						} else {
							Toast.makeText(activity, "获取数据失败！", 0).show();
						}

					}
				});
			}
			
			public void onError(Exception e,
					QjResult<Map<String, DeptEntity>> result) {
				super.onError(e, result);
				if(e != null ) CommonUtils.showErrorAtBottom(activity, handler, e.getMessage());
			};
			
			@Override
			public void onFailure(
					QjResult<Map<String, DeptEntity>> result) {
				super.onFailure(result);
				if(result != null ) CommonUtils.showErrorAtBottom(activity, handler, result.getErrorMsg());
			}
			
			@Override
			public void onHttpError(int msg) {
				super.onHttpError(msg);
				CommonUtils.showHttpError(activity, handler, msg);
			}

			public void onCompleted(Exception e,
					QjResult<Map<String, DeptEntity>> result) {
				super.onCompleted(e, result);
				LoadingDialog.getInstance(activity).dismiss();
			};
		};
		
		String path = new DataHandler().getDataObject(activity, deptUrl, null,
				new TypeToken<QjResult<Map<String, DeptEntity>>>() {
				}, callback);
	}

}
